package com.th.campusmatchbackend.manager;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.th.campusmatchbackend.config.OSSClientConfig;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * oss 对象存储操作
 */
@Component
public class OSSManager {

    @Resource
    private OSSClientConfig ossClientConfig;

    @Resource
    private OSSClient ossClient;


    /**
     * 上传对象
     * @param key
     * @param multipartFile
     * @return
     * @throws IOException
     */
    public PutObjectResult putObject(String key,MultipartFile multipartFile) throws IOException {

            // 获取上传的文件的输入流
            InputStream inputStream = multipartFile.getInputStream();
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossClientConfig.getBucketName(),key,inputStream);

            return ossClient.putObject(putObjectRequest);


    }

    /**
     * 上传对象
     * @param key
     * @param localFilePath
     * @return
     * @throws IOException
     */
    public PutObjectResult putObject(String key,String localFilePath) throws IOException {

        InputStream inputStream = new FileInputStream(new File(localFilePath));
        return ossClient.putObject(ossClientConfig.getBucketName(),key,inputStream);


    }



}
